﻿NHOMNGUOIDUNG = function () {

}
NHOMNGUOIDUNG.columns = new Array("Mã Nhóm", "Tên Nhóm");
NHOMNGUOIDUNG.fieldText = new Array("MaNhom", "TenNhom");
NHOMNGUOIDUNG.MaNhom = -1;
NHOMNGUOIDUNG.isInsert = 0;
var numberPage = 1;
NHOMNGUOIDUNG.arrays = new Array();

NHOMNGUOIDUNG.Insert = function () {
    var serviceUrl = Constants.service + "insertTblNhomNguoiDungs";
    if (Constants.OnSubmit(NHOMNGUOIDUNG.arrays) == false) {
        return false;
    }
    var data = {
        MaNhom: $("#MaNhom").val(),
        TenNhom: $("#TenNhom").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            NHOMNGUOIDUNG.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.Init = function () {
    NHOMNGUOIDUNG.arrays = new Array();
    var obj;
    obj = new CheckField("TenNhom", 1, "ErrTenNhom");
    NHOMNGUOIDUNG.Add(obj);
}

NHOMNGUOIDUNG.Add = function (obj) {
    if (NHOMNGUOIDUNG.arrays.contains(obj) == false) {
        NHOMNGUOIDUNG.arrays.push(obj);
    }
}
NHOMNGUOIDUNG.Delete = function (id) {    
    var data = {
        MaNhom: id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblNhomNguoiDungs";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            NHOMNGUOIDUNG.GetData();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.UpdateItem = function () {
    if (Constants.OnSubmit(NHOMNGUOIDUNG.arrays) == false) {
        return false;
    }
    var serviceUrl = Constants.service + "updateTblNhomNguoiDungs";
    var data = {
        MaNhom: $("#MaNhom").val(),
        TenNhom: $("#TenNhom").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {            
            NHOMNGUOIDUNG.GetData();
            Constants.Cancel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.CreateTable = function (r, nbpage, data) {
    var myTable = NHOMNGUOIDUNG.TitleColumns(NHOMNGUOIDUNG.columns);
    myTable += NHOMNGUOIDUNG.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

NHOMNGUOIDUNG.GetData = function () {
    NHOMNGUOIDUNG.MaNhom = -1;
    NHOMNGUOIDUNG.isInsert = 0;
    var serviceUrl = Constants.service + "getAllTblNhomNguoiDungs";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblNhomNguoiDungsResult;
            var dataTables = results;
            NHOMNGUOIDUNG.CreateTable(Constants.rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

NHOMNGUOIDUNG.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;
    console.info("NHOMNGUOIDUNG.DataColumnsOftr");
    console.dir(data);
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaNhom;
        myDataColumns += "<tr onclihoilamgihoick='return NHOMNGUOIDUNG.OnClick(" + id + ")'"  +" id=\"" + id + "\">";        
        myDataColumns += "<td>" + data[i].MaNhom + "</td>"
        myDataColumns += "<td>" + data[i].TenNhom + "</td>"
        myDataColumns += "<td><img onclick='return NHOMNGUOIDUNG.EditItem("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return NHOMNGUOIDUNG.Delete("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}

NHOMNGUOIDUNG.OnClick = function (id) {
    NHOMNGUOIDUNG.MaNhom = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
    return false;
}

NHOMNGUOIDUNG.OnClickEdit = function () {
    if (NHOMNGUOIDUNG.MaNhom == -1) { 
        alert(Constants.selectEdit);
    }
    else
        NHOMNGUOIDUNG.EditItem(NHOMNGUOIDUNG.MaNhom);
}

NHOMNGUOIDUNG.OnClickDelete = function () {
    if (NHOMNGUOIDUNG.MaNhom == -1) {
        alert(Constants.selectDelete);
    }
    else
        NHOMNGUOIDUNG.Delete(NHOMNGUOIDUNG.MaNhom);
}

NHOMNGUOIDUNG.EditItem = function (id) {   
    obj_id = parseInt(id);
    var temp_item = "Modules/nnd/Edit";   
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            NHOMNGUOIDUNG.GetItem(obj_id);
        }
    });
}


NHOMNGUOIDUNG.GetItem = function (id) {
    var serviceUrl = Constants.service + "getItemByIdTblNhomNguoiDung";
    var data = { MaNhom: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblNhomNguoiDungResult;
            if (result != null && result != undefined) {
                $("#MaNhom").val(result.MaNhom);
                $("#TenNhom").val(result.TenNhom);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.Clear = function () {
    Constants.clearAll(NHOMNGUOIDUNG.fieldText);
}

NHOMNGUOIDUNG.AddNewItem = function () {
    var temp_item = "Modules/nnd/AddNew";
    NHOMNGUOIDUNG.isInsert = 1;
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
            NHOMNGUOIDUNG.getMa();
        }
    });
}

NHOMNGUOIDUNG.getMa = function () {
    var serviceUrl = Constants.service + "getMaTblNhomNguoiDung";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblNhomNguoiDungResult;
            if (result != null && result != undefined) {
                $("#MaNhom").val(result);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

NHOMNGUOIDUNG.Enter = function () {
    $("#MaNhom").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#TenNhom").focus();
            event.preventDefault();
        }
    });
    $("#TenNhom").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            if (NHOMNGUOIDUNG.isInsert == 1) {
                NHOMNGUOIDUNG.Insert();
            }
            else {
                NHOMNGUOIDUNG.UpdateItem();
            }
        }
    });
}

NHOMNGUOIDUNG.Search = function () {
    var serviceUrl = Constants.service + "getFindTblNhomNguoiDungs";
    var data = {
        value: $("#search").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl,
        data: json,
        contentType: "application/json",
        dataType: "json",
        cache: false,
        success: function (msg) {
            var results = msg.getFindTblNhomNguoiDungsResult;
            if (results != null && results.length > 0) {
                NHOMNGUOIDUNG.CreateTable(Constants.rows, numberPage, results);
            }
            else {
                $("#div_list").children().remove();
                $("#div_list").html(Constants.find);
            }
        }
    });
}

$(document).ready(function () {
    NHOMNGUOIDUNG.GetData();
    NHOMNGUOIDUNG.Init();
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            NHOMNGUOIDUNG.Search();
        }
    });
    $("#search_on").click(function (event) {
        event.preventDefault();
        NHOMNGUOIDUNG.Search();
    });
});